/**
 * 获取uuid
 */
export function getUUID() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    /* eslint no-bitwise: ["error", { "allow": ["|"] }] */
    return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16);
  });
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate(data, id = 'id', pid = 'pid') {
  const res = [];
  const temp = {};
  for (let i = 0; i < data.length; i += 1) {
    temp[data[i][id]] = data[i];
  }
  for (let k = 0; k < data.length; k += 1) {
    if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
      res.push(data[k]);
      // eslint-disable-next-line no-continue
      continue;
    }
    if (!temp[data[k][pid]].children) {
      temp[data[k][pid]].children = [];
    }
    temp[data[k][pid]].children.push(data[k]);
    /* eslint no-underscore-dangle: ["error", { "allow": ["_level"] }] */
    // eslint-disable-next-line no-param-reassign
    data[k]._level = (temp[data[k][pid]]._level || 0) + 1;
  }
  return res;
}
